package gui.components.JXTable.test;

import java.io.FileInputStream;
import java.io.FilePermission;
import java.io.IOException;
import java.security.GuardedObject;

/*
java.io.FilePermission read, write, execute
java.util.PropertyPermission read, write
java.net.SocketPermission connect, resolve
java.lang.RuntimePermission null
*/

public class testGuardObject
{
    public static class Consumer implements Runnable{
        private Producer m_producer = null;

        public void link(Producer producer){
            if (m_producer != producer){
                m_producer = producer;
                m_producer.link(this);
            }
        }

        public void run(){
            for (int i = 0; i < 10; i++){
                Object object = m_producer.retrieveLocation().getObject();
                
            }
        }
    }

    public static class Producer implements Runnable{
        private Consumer m_consumer = null;

        public void link(Consumer consumer){
            if (m_consumer != consumer)
            {
                m_consumer = consumer;
                m_consumer.link(this);
            }
        }

        private String m_stringLocation = null;

        public synchronized GuardedObject retrieveLocation(){
            String stringLocation = null;
            try{
                while (m_stringLocation == null) wait();
                stringLocation = m_stringLocation;
                m_stringLocation = null;
                notifyAll();
            }
            catch (InterruptedException interruprtedexception){}
            try{
                /*
                Permission perm = new SocketPermission();
                Permission perm = new FilePermission();
                Permission perm = new RuntimePermission();
                */
                GuardedObject guardedobject = new GuardedObject(new FileInputStream(stringLocation),new FilePermission(stringLocation, "read"));
                return guardedobject;
            }
            catch (IOException ioexception){ioexception.printStackTrace();}
            return null;
        }

        private synchronized void storeLocation(String stringLocation) throws InterruptedException{
            while (m_stringLocation != null) wait();
            m_stringLocation = stringLocation;
            notifyAll();
        }

        public void run(){
            while (true){
                try{
                    storeLocation("/tmp/out.tmp");
                }
                catch (InterruptedException interruptedexception){
                    break;
                }
            }
        }

    }
}